package org.wukongcrm.converter;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.CompositeConverter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ColorConverter extends CompositeConverter<ILoggingEvent> {

    private static final String RESET = "\033[0m";
    private static final String RED_BACKGROUND = "\033[41m"; // 红色背景
    private static final String GREEN_BACKGROUND = "\033[42m"; // 绿色背景
    private static final String YELLOW_BACKGROUND = "\033[43m"; // 黄色背景
    private static final String BLUE_BACKGROUND = "\033[44m"; // 蓝色背景
    private static final String MAGENTA_BACKGROUND = "\033[45m"; // 品红色背景
    private static final String CYAN_BACKGROUND = "\033[46m"; // 青色背景
    private static final String WHITE_BACKGROUND = "\033[47m"; // 白色背景
    private static final String BLACK = "\033[30m"; // 黑色字体
    private static final String RED = "\033[31m"; // 红色字体
    private static final String GREEN = "\033[32m"; // 绿色字体
    private static final String YELLOW = "\033[33m"; // 黄色字体
    private static final String BLUE = "\033[34m"; // 蓝色字体
    private static final String MAGENTA = "\033[35m"; // 品红色字体
    private static final String CYAN = "\033[36m"; // 青色字体
    private static final String WHITE = "\033[37m"; // 白色字体

    @Override
    protected String transform(ILoggingEvent event, String in) {
        Level level = event.getLevel();
        String color = RESET;
        char levelChar;
        if (level.isGreaterOrEqual(Level.ERROR)) {
            color = RED_BACKGROUND + BLACK;
            levelChar = 'E';
        } else if (level.isGreaterOrEqual(Level.WARN)) {
            color = YELLOW_BACKGROUND + GREEN;
            levelChar = 'W';
        } else if (level.isGreaterOrEqual(Level.INFO)) {
            color = BLUE_BACKGROUND + WHITE;
            levelChar = 'I';
        } else if (level.isGreaterOrEqual(Level.DEBUG)) {
            color = GREEN_BACKGROUND + CYAN;
            levelChar = 'D';
        } else {
            color = CYAN_BACKGROUND + MAGENTA;
            levelChar = 'T';
        }
        return color + "  "  + levelChar + "  "  + RESET;
    }
}
